// request.js
class RequestUtil {
  // static baseUrl = 'http://xiaochengxu.cn'; // 替换为你的基础 URL
  static baseUrl = 'https://jsq.yangbanzhang.cn'; // 替换为你的基础 URL

  static async get(url, params) {
    try {
      const fullUrl = this.baseUrl + url + this.buildQueryString(params);
      const res = await uni.request({
        url: fullUrl,
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        }
      });
      return res.data;
    } catch (error) {
      console.error('GET 请求失败:', error);
      throw error;
    }
  }

  static async post(url, data) {
    try {
      const res = await uni.request({
        url: this.baseUrl + url,
        method: 'POST',
        data: data,
        header: {
          'Content-Type': 'application/json'
        }
      });
      return res.data;
    } catch (error) {
      console.error('POST 请求失败:', error);
      throw error;
    }
  }

  static buildQueryString(params) {
    if (!params) {
      return '';
    }
    const query = Object.keys(params).map(key => `${key}=${encodeURIComponent(params[key])}`).join('&');
    return '?' + query;
  }
}

export default RequestUtil;